/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：ReqCfgService.java
 * History:
 *         2012-6-7: Initially created, Chrise.
 */
package com.xteam.ecsystem.service.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.base.NormalState;
import com.xteam.ecsystem.dao.IReqCfgDao;
import com.xteam.ecsystem.entities.SysRequestConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 请求配置服务层。
 * @author Chrise
 */
@Service
public class ReqCfgService {
	
	@Autowired
	private IReqCfgDao rcDao;
	
	/**
	 * 获取请求配置。
	 * @author Chrise 2012-6-7
	 * @param srcName
	 * @param srcState
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public PageCenterStation<SysRequestConfig> getRequestConfigs(String srcName, int srcState, int pageIndex, int pageSize) {
		if (srcName == null || "".equals(srcName)) srcName = IReqCfgDao.INVALID_NAME;
		if (srcState == NormalState.ALL.value()) srcState = IReqCfgDao.INVALID_STATE;
		
		return this.rcDao.getConfigsByNameAndState(srcName, srcState, pageIndex * pageSize, pageSize);
	}
	
	/**
	 * 切换状态。
	 * @author Chrise 2012-6-8
	 * @param srcId
	 * @param srcTo
	 */
	public void switchState(String srcId, boolean srcTo) {
		if (srcTo) {
			this.rcDao.updateState(srcId, NormalState.EFFECTIVE.value());
		} else {
			this.rcDao.updateState(srcId, NormalState.INVALID.value());
		}
	}
	
	/**
	 * 删除请求配置。
	 * @author Chrise 2012-6-11
	 * @param ids
	 */
	public void delRequestConfigs(List<String> ids) {
		this.rcDao.deleteConfigs(ids);
	}
	
	/**
	 * 保存请求配置。
	 * @author Chrise 2012-6-12
	 * @param src
	 * @return
	 */
	public boolean saveRequestConfig(SysRequestConfig src) {
		String srcRelative = "";
		String srcTemplate = "";
		
		if (src.getSrcTemplate() != null && !"".equals(src.getSrcTemplate())) {
			srcTemplate = src.getSrcTemplate();
			
			int index = srcTemplate.indexOf("/", 1);
			if (index > 0) {
				srcRelative = srcTemplate.substring(0, index);
				srcTemplate = srcTemplate.substring(index);
			}
		}
		
		src.setSrcRelative(srcRelative);
		src.setSrcTemplate(srcTemplate);
		
		if (src.getSrcState() == null) {
			src.setSrcState(NormalState.EFFECTIVE.value());
		}
		
		String key = src.getSrcRelative().substring(1) + src.getSrcRequest();
		if (src.getSrcId() == null || "".equals(src.getSrcId())) {
			if (this.rcDao.isExist(key)) return false;
			
			this.rcDao.insertConfig(src);
		} else {
			if (this.rcDao.isExist(src.getSrcId(), key)) return false;
			
			this.rcDao.updateConfig(src);
		}
		
		return true;
	}
	
	/**
	 * 获取请求配置。
	 * @author Chrise 2012-6-12
	 * @param id
	 * @return
	 */
	public SysRequestConfig getRequestConfig(String id) {
		SysRequestConfig src = this.rcDao.getConfig(id);
		
		if (src != null) {
			String srcRelative = (src.getSrcRelative() == null) ? "" : src.getSrcRelative();
			String srcTemplate = (src.getSrcTemplate() == null) ? "" : src.getSrcTemplate();
			src.setSrcTemplate(srcRelative + srcTemplate);
		}
		
		return src;
	}
}
